package com.haust.passport.controller;

import com.haust.commons.exception.DaoException;
import com.haust.commons.pojo.HaustResult;
import com.haust.passport.service.PassportService;
import com.haust.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin
public class PassportController {

    @Autowired
    private PassportService passportService;

    @GetMapping("/user/checkUserInfo/{value}/{flag}")
    public HaustResult checkUserInfo(@PathVariable("value") String value, @PathVariable("flag") int flag) {
        return passportService.checkUserInfo(value, flag);
    }
    @PostMapping("/user/userRegister")
    public HaustResult userRegister(TbUser user) {
        try {
            return  passportService.userRegister(user);
        }catch (DaoException e){
            return HaustResult.error(e.getMessage());
        }
    }


    @PostMapping("/user/userLogin")
    public HaustResult userLogin(String username, String password, HttpSession session) {
        HaustResult result = passportService.userLogin(username, password);
        int status = result.getStatus();
        if (status == 200) {
            session.setAttribute("loginUser", result.getData());
            return result;
        } else {
            //登录失败
            return result;
        }
    }
    @PostMapping("/user/logout")
    public HaustResult logout(HttpSession session){
        session.invalidate();
        return HaustResult.ok();
    }

}
